<template>
  <div class="chart_right1">
    <div class="chart_right1-title">5年离退休预测</div>
    <div id="chart_right1" />
  </div>
</template>

<script>
const echarts = require('echarts')
import Sever from '@/api/selfApi'
export default {
  props: {
    dep_code: {
      type: String,
      default: '450000-00-0000',
      require: true
    }
  },
  data() {
    return {
      option: {
        legend: {
          textStyle: {
            color: '#fff',
            align: 'right'
          },
          x: 'right'
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            lineStyle: {
              color: {
                type: 'linear',
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [
                  {
                    offset: 0,
                    color: 'rgba(0, 255, 233,0)'
                  },
                  {
                    offset: 0.5,
                    color: 'rgba(255, 255, 255,1)'
                  },
                  {
                    offset: 1,
                    color: 'rgba(0, 255, 233,0)'
                  }
                ],
                global: false
              }
            }
          }
        },
        grid: {
          top: '10%',
          left: '3%',
          right: '3%',
          bottom: '5%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          axisLine: {
            show: true
          },
          splitArea: {
            // show: true,
            color: '#f00',
            lineStyle: {
              color: '#f00'
            }
          },
          axisLabel: {
            color: '#fff'
          },
          splitLine: {
            show: false
          },
          boundaryGap: false,
          data: []
          // date:getVirtulData()['data'],
        },
        yAxis: [
          {
            type: 'value',
            min: 0,
            // max: 140,
            splitNumber: 4,
            splitLine: {
              show: false,
              lineStyle: {
                color: 'rgba(255,255,255,0.1)'
              }
            },
            axisLine: {
              show: false
            },
            axisLabel: {
              show: false,
              margin: 20,
              textStyle: {
                color: '#d1e6eb'
              }
            },
            axisTick: {
              show: false
            }
          }
        ],
        series: [
          {
            name: '离退休',
            type: 'line',
            smooth: true, // 是否平滑
            showAllSymbol: true,
            // symbol: 'image://./static/images/guang-circle.png',
            symbol: 'circle',
            symbolSize: 25,
            lineStyle: {
              normal: {
                color: '#6c50f3',
                shadowColor: 'rgba(0, 0, 0, .3)',
                shadowBlur: 0,
                shadowOffsetY: 5,
                shadowOffsetX: 5
              }
            },
            label: {
              show: true,
              position: 'top',
              textStyle: {
                color: '#6c50f3',
                fontWeight: 'bolder'
              }
            },
            itemStyle: {
              color: '#6c50f3',
              borderColor: '#fff',
              borderWidth: 3,
              shadowColor: 'rgba(0, 0, 0, .3)',
              shadowBlur: 0,
              shadowOffsetY: 2,
              shadowOffsetX: 2
            },
            tooltip: {
              show: false
            },
            areaStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(
                  0,
                  0,
                  0,
                  1,
                  [
                    {
                      offset: 0,
                      color: 'rgba(108,80,243,0.3)'
                    },
                    {
                      offset: 1,
                      color: 'rgba(108,80,243,0)'
                    }
                  ],
                  false
                ),
                shadowColor: 'rgba(108,80,243, 0.9)',
                shadowBlur: 20
              }
            },
            data: []
          }
        ]
      },
      date_list: ['2022', '2023', '2024', '2025', '2026'],
      NO2_data: [0, 0, 0, 0, 0],
      depcode: '450000-00-0000'
    }
  },

  watch: {
    dep_code(newValue, oldValue) {
      if (newValue) {
        (this.depcode = newValue), this.getLtxyc()
      }
    }
  },
  destroyed() {
    window.onresize = null
  },
  mounted() {
    // this.getLtxyc()
  },
  methods: {
    async getLtxyc() {
      try {
        // 请求后台接口
        const res = await Sever.analyse.getLtxyc({
          // 请求参数
          dep_code: this.depcode
        })
        if (res.length == 0) {
          // 初始化data中的数值
          Object.assign(this.$data, this.$options.data())
          this.option.xAxis.data = this.date_list
          this.option.series[0].data = this.NO2_data
        } else {
          this.option.xAxis.data = this.date_list
          this.option.series[0].data = res[1]
        }
        this.drawPie()
      } catch (error) {
        console.log(error)
        Object.assign(this.$data, this.$options.data())
        this.option.xAxis.data = this.date_list
        this.option.series[0].data = this.NO2_data
        this.drawPie()
      }
    },
    drawPie() {
      const myChartPiechart_right1 = echarts.init(
        document.getElementById('chart_right1')
      )
      myChartPiechart_right1.clear()

      myChartPiechart_right1.setOption(this.option)
      // -----------------------------------------------------------------
      // 响应式变化
      window.addEventListener(
        'resize',
        () => myChartPiechart_right1.resize(),
        false
      )
    }
  }
}
</script>
<style lang="scss" scoped>
.chart_right1 {
  width: 100%;
  height: 32%;
  box-sizing: border-box;
  margin-bottom: 20px;
  // background-color: rgba(255, 255, 255, 0.03);
  border-top: 2px solid rgba(1, 153, 209, 0.5);
  display: flex;
  flex-direction: column;

  .chart_right1-title {
    font-weight: bold;
    height: 40px;
    display: flex;
    align-items: center;
    font-size: 18px;
    justify-content: center;
  }

  #chart_right1 {
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>
